<?php

class InputBuilder {

    public $fieldsArray = array();
    public $db;
    public $sql;

    function build($tableName, $pkRecord = NULL, $tab = NULL) {
        $rs = array();
        $order = "ORD,FK_TAB";
        if ($pkRecord != NULL) {
            $sql = "SELECT A.CONTROL_TYPE,A.CAPTION,A.NAME,A.LONG,B.VALUE FROM cllg_tablefield A LEFT JOIN 
	  		cllg_table_valuebyfield B ";
            $sql.=sprintf("ON (A.NAME=B.FK_TABLEFIELD AND B.PK_RECORD='%s') ", $pkRecord);
            $sql.=sprintf("WHERE A.TABLE_NAME='%s' AND A.STATUS=1 ", $tableName);
            if ($tab != NULL) {
                $sql.=sprintf(" AND A.FK_TAB=%s ", $tab);
            }
            $this->sql = $sql . " ORDER BY " . $order . " ASC";
            $rs = $this->db->fetch_all_array($this->db->query($sql));
        } else {
            $where = sprintf("TABLE_NAME='%s'", $tableName);
            $where.=" AND STATUS=1";
            if ($tab != NULL) {
                $where.=sprintf(" AND FK_TAB=%s ", $tab);
            }
            $rs = $this->db->query_select("cllg_tablefield", $where, $order);
        }
        foreach ($rs as $row) {
            switch ($row['CONTROL_TYPE']) {
                case 'input':
                    $this->buildFormField($row['CAPTION'], $this->buildInput($row, $fill));
                    break;
                case 'checkbox':
                    $this->buildFormField($row['CAPTION'], $this->buildCheck($row, $fill));
                    break;
                case 'combo':
                    $this->buildFormField($row['CAPTION'], $this->buildComboBox($row, $fill));
                    break;
                case 'txarea':
                    $this->buildFormField($row['CAPTION'], $this->buildTxArea($row, $fill));
                    break;
                case 'sep':
                    $this->buildSeparator();
                    break;
            }
            $this->fieldsArray[] = $row['NAME'];
        }
    }

    function buildInput($row, $filled = false) {

        $input = '<input name="' . $row['NAME'] . '" maxlength="' . $row['LONG']
                . '" size="' . $row['LONG']
                . '" id="' . $row['NAME']
                . '" size="" value="'
                . (filled ? $row['VALUE'] : "") . '" type="' . $row['TYPE'] . '" />';
        return $input;
    }

    function buildTxArea($row, $filled = false) {
        $input = ' <textarea name="' . $row['NAME'] . '" rows="5" cols="50" id="' . $row['NAME']
                . '" size="" >' . (filled ? (isset($row['VALUE']) ? $row['VALUE'] : "" ) : "") . '</textarea>';
        return $input;
    }

    function buildComboBox($row, $filled = false) {
        return '<select name="' . $row['NAME'] . '" size="1" id="' . $row['NAME'] . '">' .
                '<option value="">vacio</option> ' . '</select>';
    }

    function buildFormField($label, $input) {
        echo '<tr>';
        echo '  <td width="25%">' . $label . '</td>';
        echo '<td width="25%">';
        echo $input;
        echo '</td></tr>';
    }

    function buildCheck($row, $filled = false) {
        $input = '<input name="' . $row['NAME'] . '" id="' . $row['NAME'] . '" value="1" type="checkbox" ' .
                ($filled == true ? 'checked="checked" ' : '') . '/>';
        return $input;
    }

}

?>